package  
{
	import CharacterTraits.Name;
	import org.flixel.plugin.photonstorm.FlxExtendedSprite;
	import org.flixel.FlxG;
	import org.flixel.FlxU;
	import Data.Color;
	import ConversationClasses.*;
	
	public class Player extends FlxExtendedSprite
	{
		private const _width:uint = 30;
		private const _height:uint = 30;
		private const _playerMoveSpeed:uint = 8000;
		
		public static var name:Name;
		
		public var looking:String = "";
		private var tempFrameNum:Number;
		
		private var lastX:Number;
		private var lastY:Number;
		private var deltaX:Number;
		private var deltaY:Number;
		
		public function Player(X:Number, Y:Number, SimpleGraphic:Class) 
		{
			super(X, Y);
			
			loadGraphic(SimpleGraphic, true, false, 30, 30);
			createAnimations();
		} 
		
		override public function update():void 
		{
			super.update();
			
			ResetVelocity();
			
			deltaX = x - lastX;
			deltaY = y - lastY;
			lastX = x;
			lastY = y;
			
			if (!Conversation.inConversation && !BaseGameState.invVisable)
			{
				Move();
			}
			
			updateAnimations();
		}
		
		private function createAnimations():void 
		{
			addAnimation("idle_up", [4]);
			addAnimation("idle_right", [10]);
			addAnimation("idle_down", [1]);
			addAnimation("idle_left", [7]);
			addAnimation("idle_upRight", [13]);
			addAnimation("idle_downRight", [22]);
			addAnimation("idle_downLeft", [16]);
			addAnimation("idle_upLeft", [19]);
			addAnimation("walk_up", [5, 4, 3, 4], 6); // 5 = frames per second for this animation
			addAnimation("walk_right", [11, 10, 9, 10], 6);
			addAnimation("walk_down", [0, 1, 2, 1], 6);
			addAnimation("walk_left", [6, 7, 8, 7], 6);
			addAnimation("walk_upRight", [12, 13, 14, 13], 6);
			addAnimation("walk_downRight", [23, 22, 21, 22], 6);
			addAnimation("walk_downLeft", [17, 16, 15, 16], 6);
			addAnimation("walk_upLeft", [18, 19, 20, 19], 6);
		}
		
		public function updateAnimations():void
		{
			if (deltaX == 0 && deltaY < -1)
			{
				if (looking != "up")
				{
					tempFrameNum = frame;
				}
				play("walk_up", false, false);
				looking = "up";
			}
			else if (deltaX > 1 && deltaY == 0)
			{
				if (looking != "right")
				{
					tempFrameNum = frame;
				}
				play("walk_right", false, false);
				looking = "right";
			}
			else if (deltaX == 0 && deltaY > 1)
			{
				if (looking != "down")
				{
					tempFrameNum = frame;
				}
				play("walk_down", false, false);
				looking = "down";
			}
			else if (deltaX < -1 && deltaY == 0)
			{
				if (looking != "left")
				{
					tempFrameNum = frame;
				}
				play("walk_left", false, false);
				looking = "left";
			}
			else if (deltaX > 1 && deltaY < -1)
			{
				if (looking != "upRight")
				{
					tempFrameNum = frame;
				}
				play("walk_upRight", false, false);
				looking = "upRight";
			}
			else if (deltaX > 1 && deltaY > 1)
			{
				if (looking != "downRight")
				{
					tempFrameNum = frame;
				}
				play("walk_downRight", false, false);
				looking = "downRight";
			}
			else if (deltaX < -1 && deltaY > 1)
			{
				if (looking != "downLeft")
				{
					tempFrameNum = frame;
				}
				play("walk_downLeft", false, false);
				looking = "downLeft";
			}
			else if (deltaX < -1 && deltaY < -1)
			{
				if (looking != "upLeft")
				{
					tempFrameNum = frame;
				}
				play("walk_upLeft", false, false);
				looking = "upLeft";
			}
			else if (deltaX == 0 && deltaY == 0)
			{
				if ((FlxG.keys.pressed('D') || FlxG.keys.pressed("RIGHT")) && (FlxG.keys.pressed('W') || FlxG.keys.pressed("UP")))
				{
					looking = "upRight";
					play("idle_upRight");
				}
				else if ((FlxG.keys.pressed('A') || FlxG.keys.pressed("LEFT")) && (FlxG.keys.pressed('W') || FlxG.keys.pressed("UP")))
				{
					looking = "upLeft";
					play("idle_upLeft");
				}
				else if ((FlxG.keys.pressed('D') || FlxG.keys.pressed("RIGHT")) && (FlxG.keys.pressed('S') || FlxG.keys.pressed("DOWN")))
				{
					looking = "downRight";
					play("idle_downRight");
				}
				else if ((FlxG.keys.pressed('A') || FlxG.keys.pressed("LEFT")) && (FlxG.keys.pressed('S') || FlxG.keys.pressed("DOWN")))
				{
					looking = "downLeft";
					play("idle_downLeft");
				}
				else if (FlxG.keys.pressed('W') || FlxG.keys.pressed("UP"))
				{
					looking = "up";
					play("idle_up");
				}
				else if (FlxG.keys.pressed('S') || FlxG.keys.pressed("DOWN"))
				{
					looking = "down";
					play("idle_down");
				}
				else if (FlxG.keys.pressed('A') || FlxG.keys.pressed("LEFT"))
				{
					looking = "left";
					play("idle_left");
				}
				else if (FlxG.keys.pressed('D') || FlxG.keys.pressed("RIGHT"))
				{
					looking = "right";
					play("idle_right");
				}
				else
				{
					if (looking == "right")
						play("idle_right");
					else if (looking == "down")
						play("idle_down");
					else if (looking == "left")
						play("idle_left");
					else if (looking == "upRight")
						play("idle_upRight");
					else if (looking == "downRight")
						play("idle_downRight");
					else if (looking == "downLeft")
						play("idle_downLeft");
					else if (looking == "upLeft")
						play("idle_upLeft");
					else
						play("idle_up");
				}
			}
		}
		
		public function setName(playerName:Name):void
		{
			name = playerName;
		}
		
		public function ResetVelocity():void
		{
			velocity.x = 0;
			velocity.y = 0;
		}
		
		public function Move():void 
		{
			//get input and update velocity
			if (FlxG.keys.pressed('W') || FlxG.keys.pressed("UP"))
			{
				velocity.y -= _playerMoveSpeed * FlxG.elapsed;
			}
			if (FlxG.keys.pressed('S') || FlxG.keys.pressed("DOWN"))
			{
				velocity.y += _playerMoveSpeed * FlxG.elapsed;
			}
			if (FlxG.keys.pressed('A') || FlxG.keys.pressed("LEFT"))
			{
				velocity.x -= _playerMoveSpeed * FlxG.elapsed;
			}
			if (FlxG.keys.pressed('D') || FlxG.keys.pressed("RIGHT"))
			{
				velocity.x += _playerMoveSpeed * FlxG.elapsed;
			}
			
			if (velocity.x != 0 && velocity.y != 0)
			{
				velocity.x *= .7;
				velocity.y *= .7;
			}
		}
		
		public function KeepOnScreen():void
		{
			if (x <= 0)
			{
				x = 0;
			}
			
			if (x + width >= FlxG.width)
			{
				x = FlxG.width - width;
			}
			
			if (y <= 0)
			{
				y = 0;
			}
			
			if (y + height >= FlxG.height)
			{
				y = FlxG.height - height;
			}
		}
	}
}